//
//  MZExtensionLayer.swift
//  MZTool
//
//  Created by gby on 2022/3/9.
//

import Foundation
import QuartzCore


extension MZKit where Base == CALayer {
    //MARK: 渐变
    public static func create_GradientLayer(_ size: CGSize, _ data: (statp: CGPoint, endp: CGPoint, colors:[UIColor], locations: [NSNumber]) ) -> CAGradientLayer{
        let gl = CAGradientLayer.init()
        gl.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height);
        gl.startPoint = data.statp
        gl.endPoint = data.endp
        let cgcolors: [CGColor] = data.colors.map { (color) -> CGColor in
            return color.cgColor
        }
        gl.colors = cgcolors
        gl.locations = data.locations
        return gl
    }
    
    //MARK: 虚线
    public static func create_CAShapLayer(_ bounds: CGRect, _ data: (linecolor: UIColor, linelength:Float, linespace: Float) ) -> CAShapeLayer{
        let shap = CAShapeLayer.init()
        shap.bounds = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height);
        shap.position = CGPoint(x: bounds.width/2, y: bounds.height)
        shap.fillColor = data.linecolor.cgColor
        shap.strokeColor = data.linecolor.cgColor
        shap.lineWidth = bounds.height
        shap.lineJoin = CAShapeLayerLineJoin.round
        shap.lineDashPattern = [NSNumber.init(value: data.linelength), NSNumber.init(value: data.linespace)]
        let path = CGMutablePath.init()
        path.move(to: CGPoint.zero)
        path.addLine(to: CGPoint(x: bounds.width, y: 0))
        shap.path = path
        return shap
    }
    
    //MARK: 三角
    public static func create_TriangleLayer(_ bounds: CGRect, _ data: (p1: CGPoint, p2: CGPoint, p3: CGPoint, pcolor:UIColor)) -> CAShapeLayer{
        let tlayer = CAShapeLayer.init()
        
        let trianglePath = UIBezierPath.init()
        trianglePath.move(to: data.p1)
        trianglePath.addLine(to: data.p2)
        trianglePath.addLine(to: data.p3)
        
        tlayer.frame = bounds
        tlayer.path = trianglePath.cgPath
        tlayer.lineWidth = 0.5
        tlayer.strokeColor = data.pcolor.cgColor
        tlayer.fillColor = data.pcolor.cgColor
        
        return tlayer
    }
}
